<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/5
 * Time: 上午9:43
 */
class GlobalController extends AdminBaseController {

    public function index(){
    	$this->setTitle('全服道具/货币发送列表');
    
    	$filter = new PubFilter();
    	$grid   = new PubGrid();
    
//     	$filter->addSeaOsFilter();
//     	$filter->addOssServer(true)
    	$filter->addSeaSpecialFilter(true);
    	$filter->addSeaOsFilterAll();
    	$filter->addOssServerAll()
    	->addFilter('begin', 'input', '创建时间', array('date' => 'day'))
    	->addFilter('end', 'input', '至', array('date' => 'day'));
    	
    	$statusList = GlobalService::status();
    	$statusList = array('' => '所有') + $statusList;
    	$filter->addFilter('status', 'select', '状态', array(
    			'options'   => $statusList,
    			'value'     => key($statusList)
    	));
    
    	$params = $filter->get();
    	$header = GlobalService::indexHeader();
    	$data   = GlobalService::indexData($params);
    
    	$grid->addGrid($header, $data);
    
    	$this->out['filter'] = $filter->show();
    	$this->out['grid'] = $grid->show();
    }
    
    public function approveList(){
    	$this->setTitle('全服道具/货币审核列表');
    
    	$filter = new PubFilter();
    	$grid   = new PubGrid();
    
//     	$filter->addSeaOsFilter();
//     	$filter->addOssServer(true)
    	$filter->addSeaSpecialFilter(true);
    	$filter->addSeaOsFilterAll();
    	$filter->addOssServerAll()
    	->addFilter('begin', 'input', '创建时间', array('date' => 'day'))
    	->addFilter('end', 'input', '至', array('date' => 'day'));
    	 
    	$statusList = GlobalService::status();
    	$filter->addFilter('status', 'select', '状态', array(
    			'options'   => $statusList,
    			'value'     => key($statusList)
    	));
    	
    	$params = $filter->get();
    	
    	$header = GlobalService::approveHeader();
    	if(PubFunc::isAjax()){
    		$result = GlobalService::approveData($params);
    			
    		$this->out = array(
    				'count'     => $result['count'],
    				'data'      => $result['data'],
    				'result'    => true
    		);
    		$this->outType = 'json';
    	}else{
    		$order  = 'id';
    		$params['m'] = PubRequest::R('m');
    		$url = PubFunc::ajaxUrl("?r=global/approveList", $params);
    		$grid->addGrid($header, $url, array(
    				'name'=>'数据',
    				'order'=>array($order, 'desc'),
    				'type'=>'global/approveList'
    		));
    			
    		$this->out['grid'] = $grid->show();
    		$this->out['filter'] = $filter->show();
    	}
    }
    

    public function add(){
        $this->setTitle('申请全服道具/货币发送');
        $this->out['serverList']    = ServerService::getOptions();
        $this->out['sendType']      = GlobalService::sendType();
        
        $list = PubServer::getChannelServer();
        $this->out['list'] = $list;
    }
    
    public function doAdd(){
    	$result = GlobalService::doAdd($_POST);
    		
    	if($result['state']){
    		$this->showMessage('申请成功', PubFunc::buildUrl('/oss/?r=global/index'));
    	}else{
    		$this->showMessage($result['msg']);
    	}
    }
    
    public function delete(){
    	$id = $this->R('id');
    
    	$result = GlobalService::delete($id);
    
    	if($result !== false){
    		$this->showMessage('删除成功', PubFunc::buildUrl("/oss/?r=global/index"));
    	}else{
    		$this->showMessage('删除失败');
    	}
    }
    
    public function synchro(){
    	$ids = PubRequest::R('ids');
    	if(PubFunc::isAjax()){
    		$servers = PubRequest::R('servers');
    		$result = GlobalService::synchro($ids,$servers);
    		$this->out = array(
    				'status' => 1,
    				'msg' => $result,
    				'data'    => ''
    		);
    		$this->outType = 'json';
    	}else{
    
    		$list = PubServer::getChannelServer();
    
    		$this->out['list'] = $list;
    		$this->out['ids'] = $ids;
    		$this->layout = '';
    
    		$url = PubFunc::buildUrl("/oss/?r=global/synchro");
    		$this->out['url'] = $url;
    
    
    	}
    }
    
    /**
     * 查看审批信息
     */
    public function getApproval(){
    	$this->layout = '';
    	$id = PubRequest::R('id');
    
    	$result = GlobalService::getApproval($id);
    	if(!$result){
    		$result = array();
    	}
    
    	$this->out['list']      = $result;
    }
    
    /**
     * 驳回
     */
    public function reject(){
    	$id = $this->R('id');
    
    	$result = GlobalService::reject($id);
    
    	$this->out = $result;
    	$this->outType = 'json';
    }
    
    /**
     * 批准
     */
    public function approve(){
    	$id = $this->R('id');
    
    	$result = GlobalService::approve($id);
    
    	$this->out = $result;
    	$this->outType = 'json';
    }
    
    /**
     * 批量驳回
     */
    public function batchReject(){
    	$ids = $this->R('ids');
    
    	$result = GlobalService::batchReject($ids);
    
    	$this->out = $result;
    	$this->outType = 'json';
    }
    
    /**
     * 批量批准
     */
    public function batchApprove(){
    	$ids = $this->R('ids');
    
    	$result = GlobalService::batchApprove($ids);
    
    	$this->out = $result;
    	$this->outType = 'json';
    }


}